#include "LevelCompleteScene.h"
#include "GLUtils.h"
#include "Transformation.h"
#include "PlainTexture.h"
#include "SimpleText.h"
#include "MainMenu.h"
#include "Exception.h"
#include "Logger.h"

using namespace game_engine;
using namespace game_engine::utils;
using namespace game_engine::open_gl;
using namespace game_engine::components;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

LevelCompleteScene::LevelCompleteScene(void)
    : Scene("LevelCompleteScene")
{
}

LevelCompleteScene::~LevelCompleteScene(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

const BOOL LevelCompleteScene::initialize(void)
{
    BOOL ok = true;
   
    try
    {
        // Create the objects
        createObjects();

        // Initialize all game objects
        initGameObjects();

        // Graphics initialization
        GLUtils::commonLevelInit();
        GLUtils::levelInit(resolution.getWidth(), resolution.getHeight()); 
    } catch (exception &e) {
        Logger::FATAL(GAME_LOG, string("Error during LevelCompleteScene initialization : ") + 
            e.what());
        return false;
    }

    nextScene = this;

    return ok;
}

const BOOL LevelCompleteScene::update(const FLOAT32 time)
{
    // Update the whole scene
    BOOL ok = true;
    try
    {
        updateGameObjects(time);
    } catch (exception &e)
    {
        string err("Error during scene update: ");
        err.append(e.what());
        Logger::ERR(GAME_LOG, err);
        return false;
    }

    // 'M' or 'm' key pressed
   	if (keys[77] || keys[109])
    {
        ok = false;
        MainMenu *mainMenu = new MainMenu();
        mainMenu->setResolution(Size(512, 640));
        nextScene = mainMenu;
    }
    // 'Q' or 'q' key pressed
    else if (keys[81] || keys[113])
    {
        ok = false;
        keys[113] = false;
        nextScene = NULL;
    }

    return ok;
}

void LevelCompleteScene::finalize(void)
{
    try
    {
        // Finalize game objects
        finishGameObjects();
    } catch (exception &e)
    {
        string err("Error during scene finalization: ");
        err.append(e.what());
        Logger::ERR(GAME_LOG, err);
        throw RuntimeException(err);
    }
}

///////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////

void LevelCompleteScene::createObjects(void) throw (...)
{
    // TODO : order IS VERY IMPORTANT, so objects must have a priority in order
    // to make sure that methods like initialize, update, render ... are called
    // correctly

 	///////////////////////////////////////////////////////////////////////////
    // Create the TEXTURE
    ///////////////////////////////////////////////////////////////////////////
    GameObject *texture = new GameObject(this, "LevelComplete", "", HUD_LEVEL);
    // Create its components
    Transformation *texTransf = new Transformation(texture, 256.0, 350.0, 
        0.0, 0.0, 1.0, 1.0);
    PlainTexture *texTexture = new PlainTexture(texture, "levelcomplete.png", GL_RGBA, true);
    // Add components to object
    texture->addComponent(texTransf);
    texture->addComponent(texTexture);

 	///////////////////////////////////////////////////////////////////////////
    // Create the MESSAGE
    ///////////////////////////////////////////////////////////////////////////
    GameObject *continueMessage1 = new GameObject(this, "ContinueMessage1", "Label", HUD_LEVEL);
    // Create its components
    Transformation *msgTransf1 = new Transformation(continueMessage1, 90.0, 240.0, 
        0.0, 0.0, 1.0, 1.0);
    SimpleText *msgText1 = new SimpleText(continueMessage1, HELVETICA_18, 
        ColorRGB(255.0, 255.0, 255.0));
    msgText1->setText("Press the 'M' key to return to main menu");
    // Add components to object
    continueMessage1->addComponent(msgTransf1);
    continueMessage1->addComponent(msgText1);
    
    GameObject *continueMessage2 = new GameObject(this, "ContinueMessage2", "Label", HUD_LEVEL);
    // Create its components
    Transformation *msgTransf2 = new Transformation(continueMessage2, 110.0, 200.0, 
        0.0, 0.0, 1.0, 1.0);
    SimpleText *msgText2 = new SimpleText(continueMessage2, HELVETICA_18, 
        ColorRGB(255.0, 255.0, 255.0));
    msgText2->setText("Press the 'Q' key to quit the game");
    // Add components to object
    continueMessage2->addComponent(msgTransf2);
    continueMessage2->addComponent(msgText2);
    
    addObject(texture);
    addObject(continueMessage1);
    addObject(continueMessage2);
}